﻿using System;
using System.Diagnostics;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace SuSPExtension
{
	// Token: 0x02000002 RID: 2
	public class CommonUtils
	{
		// Token: 0x06000001 RID: 1 RVA: 0x00002050 File Offset: 0x00000250
		public static string GetFileOrDirInfo(string path)
		{
			StringBuilder stringBuilder = new StringBuilder();
			if (Directory.Exists(path))
			{
				long size = 0L;
				CommonUtils.GetDirSizeByPath(path, ref size);
				DirectoryInfo directoryInfo = new DirectoryInfo(path);
				stringBuilder.AppendLine(string.Format("Last Write Time：{0}", directoryInfo.LastWriteTime));
				stringBuilder.AppendLine(CommonUtils.GetSizeString(size));
			}
			else
			{
				FileInfo fileInfo = new FileInfo(path);
				stringBuilder.AppendLine(string.Format("Current File MD5: {0}", CommonUtils.GetMD5HashFromFile(path)));
				stringBuilder.AppendLine(string.Format("Current File SHA1: {0}", CommonUtils.GetSHA1(path)));
				stringBuilder.AppendLine(string.Format("Is ReadOnly: {0}", fileInfo.IsReadOnly));
				stringBuilder.AppendLine(string.Format("Last WriteTime: {0}", fileInfo.LastWriteTime));
				stringBuilder.AppendLine(CommonUtils.GetSizeString(fileInfo.Length));
				string extension = Path.GetExtension(path);
				if (extension == ".exe" || extension == ".dll")
				{
					FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(path);
					string arg = string.Concat(new string[]
					{
						versionInfo.FileMajorPart.ToString(),
						"_",
						versionInfo.FileMinorPart.ToString(),
						"_",
						versionInfo.FileBuildPart.ToString()
					});
					stringBuilder.AppendLine(string.Format("FileVersion: {0}", arg));
					stringBuilder.AppendLine(string.Format("Architecture: {0}", CommonUtils.GetArchitectureStr(path)));
				}
			}
			return stringBuilder.ToString();
		}

		// Token: 0x06000002 RID: 2 RVA: 0x000021E0 File Offset: 0x000003E0
		public static string GetArchitectureStr(string path)
		{
			ushort pearchitecture = CommonUtils.GetPEArchitecture(path);
			if (pearchitecture == 267)
			{
				return "32bit";
			}
			if (pearchitecture == 523)
			{
				return "64bit";
			}
			return "Empty";
		}

		// Token: 0x06000003 RID: 3 RVA: 0x00002218 File Offset: 0x00000418
		public static string GetMD5HashFromFile(string fileName)
		{
			string result;
			try
			{
				FileStream fileStream = new FileStream(fileName, FileMode.Open);
				byte[] array = new MD5CryptoServiceProvider().ComputeHash(fileStream);
				fileStream.Close();
				StringBuilder stringBuilder = new StringBuilder();
				for (int i = 0; i < array.Length; i++)
				{
					stringBuilder.Append(array[i].ToString("x2"));
				}
				result = stringBuilder.ToString();
			}
			catch (Exception ex)
			{
				throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
			}
			return result;
		}

		// Token: 0x06000004 RID: 4 RVA: 0x000022A0 File Offset: 0x000004A0
		public static string GetSHA1(string path)
		{
			string result;
			try
			{
				FileStream fileStream = new FileStream(path, FileMode.Open);
				byte[] array = new SHA1CryptoServiceProvider().ComputeHash(fileStream);
				fileStream.Close();
				StringBuilder stringBuilder = new StringBuilder();
				for (int i = 0; i < array.Length; i++)
				{
					stringBuilder.Append(array[i].ToString("x2"));
				}
				result = stringBuilder.ToString();
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
				result = "";
			}
			return result;
		}

		// Token: 0x06000005 RID: 5 RVA: 0x00002324 File Offset: 0x00000524
		private static string GetSizeString(long size)
		{
			if (size < 1024L)
			{
				return string.Format("FileSize: {0} Bytes", size);
			}
			if (size < 1048576L)
			{
				return string.Format("FileSize: {0} KB", (float)size / 1024f);
			}
			if (size < 1073741824L)
			{
				return string.Format("FileSize: {0} MB", (float)size / 1048576f);
			}
			return string.Format("FileSize: {0} GB", (float)size / 1.07374182E+09f);
		}

		// Token: 0x06000006 RID: 6 RVA: 0x000023A4 File Offset: 0x000005A4
		public static void GetDirSizeByPath(string dir, ref long dirSize)
		{
			try
			{
				DirectoryInfo directoryInfo = new DirectoryInfo(dir);
				DirectoryInfo[] directories = directoryInfo.GetDirectories();
				FileInfo[] files = directoryInfo.GetFiles();
				DirectoryInfo[] array = directories;
				for (int i = 0; i < array.Length; i++)
				{
					CommonUtils.GetDirSizeByPath(array[i].FullName, ref dirSize);
				}
				foreach (FileInfo fileInfo in files)
				{
					dirSize += fileInfo.Length;
				}
			}
			catch (Exception ex)
			{
				Console.WriteLine("获取文件大小失败" + ex.Message);
			}
		}

		// Token: 0x06000007 RID: 7 RVA: 0x00002434 File Offset: 0x00000634
		public static ushort GetPEArchitecture(string pFilePath)
		{
			ushort result = 0;
			try
			{
				using (FileStream fileStream = new FileStream(pFilePath, FileMode.Open, FileAccess.Read))
				{
					using (BinaryReader binaryReader = new BinaryReader(fileStream))
					{
						if (binaryReader.ReadUInt16() == 23117)
						{
							fileStream.Seek(58L, SeekOrigin.Current);
							fileStream.Seek((long)((ulong)binaryReader.ReadUInt32()), SeekOrigin.Begin);
							if (binaryReader.ReadUInt32() == 17744U)
							{
								fileStream.Seek(20L, SeekOrigin.Current);
								result = binaryReader.ReadUInt16();
							}
						}
					}
				}
			}
			catch (Exception)
			{
			}
			return result;
		}

		// Token: 0x04000001 RID: 1
		private const int mb_size = 1048576;

		// Token: 0x04000002 RID: 2
		private const int kb_size = 1024;
	}
}
